<?php
class PsModel extends Action{
	//test
	//
	//############test
	public function add($planid,$lineid,$itemid,$planvolumebox){
		$info=collectinfo(__METHOD__,'$planid,$lineid,$itemid,$planvolumebox',array($planid,$lineid,$itemid,$planvolumebox));
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($itemid)===false){return createarrerr('error_code','itemid 不能为空',$info);}//防止NULL
		if(isset($planvolumebox)===false){return createarrerr('error_code','planvolumeboxs 不能为空',$info);}//防止NULL

		$part=D('Part');$plan=D('Plan');
		$ps=M('ps');

		$arr_plano=$plan->getplanobyplanid($planid);$plano=$arr_plano['data'];
		//线边时间非常重要，有这种算法的地方有两个，一个是配送员界面，一个是接口
		// $arr_theorypsleavetm=$this->gettheorypsleavetm($plano);$theorypsleavetm=$arr_theorypsleavetm['data'];
		

		$arr_partls=$part->getpartlsbyitemid($itemid);$partls=$arr_partls['data'];

		foreach($partls as $partv){
			$dt=array();
			$dt['f_ps_planid']=$planid;
			$dt['f_ps_lineid']=$lineid;
			$dt['f_ps_itemid']=$itemid;
			$dt['f_ps_partid']=$partv['partid'];
			//要生产多少箱成本，其中一箱成品对应多少瓶物料已经知道了
			$dt['psvolume']=$planvolumebox*$partv['partvolumepc'];
			$dt['psalready']=0;
			$dt['psremain']=0;
			
			$psalready=0;$partid=$partv['partid'];$partvolumepc=$partv['partvolumepc'];
			$arr_xianbiantm=$this->jisuanxianbiantm_psleavetm_nopsid($itemid,$planid,$psalready,$partid,$lineid,$partvolumepc);$xianbiantm=$arr_xianbiantm['data'];

			$dt['psleavetm']=$xianbiantm;
			$dt['f_ps_psstatid']=2;
			$dt['psodr']=0;
			$dt['psclick']=0;
			$ps->data($dt)->add();
		}


		return createarrok('ok',$data,'',$info);
	}

	public function gettheorypsleavetm($plano){
		$info=collectinfo(__METHOD__,'$plano',array($plano));
		if(isset($plano)===false){return createarrerr('error_code','plano 不能为空',$info);}//防止NULL
		$plan=D('Plan');

		//先确定几几年几月几日几点几分几秒
		if($plano['f_plan_banciid']==1){
			$tm_basic=$plano['plantm'].' 07:30:00';
			$tm_basic=strtotime($tm_basic);
		}else{
			$tm_basic=$plano['plantm'].' 19:30:00';
			$tm_basic=strtotime($tm_basic);
		}
		
		if($plano['planodr']==1){
			$tm=$tm_basic;
		}else{
			$tm=0;
			for($odr=1;$odr<$plano['planodr'];$odr++){
				$arr_planx=$plan->getplanobyplantmandplanodr($plano['plantm'],$odr);$planx=$arr_planx['data'];
				$tm=$tm+(float)$planx['planworktm'];
			}
			$tm=$tm_basic+3600*$tm;
		}
		//转化为剩余时间
		$seconds=$tm-time();

		return createarrok('ok',$seconds,'',$info);
	}

	public function getsumvolumebyplanid($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		$plan=D('Plan');

		//先计算计划生产多少个
		$arr_plano=$plan->getplanobyplanid($planid);$plano=$arr_plano['data'];
		$planvolumebox=$plano['planvolumebox'];

		$ps=M('ps');

		$data['sum_psvolume']=$ps->field('sum(psvolume)')->where("f_ps_planid=".$planid)->select()[0]['sum(psvolume)'];
		
		$data['sum_psalready']=$ps->field('sum(psalready)')->where("f_ps_planid=".$planid)->select()[0]['sum(psalready)'];;


		return createarrok('ok',$data,'',$info);
	}

	public function jiapei($planid,$jiapeishuliang){
		$info=collectinfo(__METHOD__,'$planid,$jiapeishuliang',array($planid,$jiapeishuliang));
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		if(isset($jiapeishuliang)===false){return createarrerr('error_code','jiapeishuliang 不能为空',$info);}//防止NULL
		

		$part=D('Part');$plan=D('Plan');
		$ps=M('ps');

		$arr_plano=$plan->getplanobyplanid($planid);$plano=$arr_plano['data'];


		$arr_partls=$part->getpartlsbyitemid($plano['f_plan_itemid']);$partls=$arr_partls['data'];

		foreach($partls as $partv){
			$dt=array();
			
			$arr_pso=$this->getpsobyplanidandpartid($planid,$partv['partid']);$pso=$arr_pso['data'];
			//要生产多少箱成本，其中一箱成品对应多少瓶物料已经知道了
			$dt['psvolume']=($plano['planvolumebox']+$jiapeishuliang)*$partv['partvolumepc'];

			// if(($plano['planvolumebox']+$jiapeishuliang)*$partv['partvolumepc']>$pso['psalready']){$dt['f_ps_psstatid']=4;}
			
			$ps->where('psid='.$pso['psid'])->setField($dt);
		}


		return createarrok('ok',$data,'',$info);
	}
	

	public function getpsobyplanidandpartid($planid,$partid){
		$info=collectinfo(__METHOD__,'$planid,$partid',array($planid,$partid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		if(isset($partid)===false){return createarrerr('error_code','partid 不能为空',$info);}//防止NULL

		$ps=M('ps');
		$pso=$ps->where('f_ps_planid='.$planid.' AND f_ps_partid='.$partid)->find();

		return createarrok('ok',$pso,'',$info);
	}

	public function getpslsbylineid($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
		
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		
		$ps=M('ps');
		$psls=$ps->join('tb_part ON f_ps_partid=partid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where('f_ps_lineid='.$lineid.' AND f_ps_psstatid<>1 AND f_ps_psstatid<>2')->select();//查看准备中的

		return createarrok('ok',$psls,'',$info);
	}

	public function getpslsbypsleavetmASC($paramaxpapertm,$parttypels){
		$info=collectinfo(__METHOD__,'$paramaxpapertm',array($paramaxpapertm));
		
		$cdt="(1=2";
		foreach($parttypels as $parttypev){
			$cdt=$cdt.' OR f_part_parttypeid='.$parttypev['parttypeid'];
		}
		$cdt=$cdt.')';
		
		$ps=M('ps');
		$psls=$ps->join('tb_plan ON f_ps_planid=planid')->join('tb_item ON f_ps_itemid=itemid')->join('tb_part ON f_ps_partid=partid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where('f_ps_psstatid<>1 AND f_ps_psstatid<>2 AND '.$cdt)->order('psleavetm ASC')->limit(0,$paramaxpapertm)->select();//查看准备中的

		return createarrok('ok',$psls,'',$info);
	}

	public function getpslsbylineidandparttypels($lineid,$parttypels){
		$info=collectinfo(__METHOD__,'$lineid,$parttypels',array($lineid,$parttypels));
		
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($parttypels)===false){return createarrerr('error_code','parttypels 不能为空',$info);}//防止NULL

		$cdt="(1=2";
		foreach($parttypels as $parttypev){
			$cdt=$cdt.' OR f_part_parttypeid='.$parttypev['parttypeid'];
		}
		$cdt=$cdt.')';
		
		$ps=M('ps');
		$psls=$ps->join('tb_plan ON f_ps_planid=planid')->join('tb_item ON f_ps_itemid=itemid')->join('tb_part ON f_ps_partid=partid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where('f_ps_lineid='.$lineid.' AND f_ps_psstatid<>1 && f_ps_psstatid<>2 AND '.$cdt)->select();//查看准备中的

		return createarrok('ok',$psls,'',$info);
	}



	public function addpeisongshuliang($psid,$defaultpeisongliang){
		$info=collectinfo(__METHOD__,'$psid,$defaultpeisongliang',array($psid,$defaultpeisongliang));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		if(isset($defaultpeisongliang)===false){return createarrerr('error_code','defaultpeisongliang 不能为空',$info);}//防止NULL
		$line=D('Line');$plan=D('Plan');$yuzhi=D('Yuzhi');$item=D('Item');

		$ps=M('ps');

		// //先不管37二十一先click为一
		// $dttt=array('psclick'=>1);
		// $this->mdf($dttt,$psid);
		// $ps=M('ps');

		


		// $arr_pso=$this->getpsobypsid($psid);$pso=$arr_pso['data'];
		// $ps=M('ps');
		// $dt=array('psalready'=>($pso['psalready']+$defaultpeisongliang));
		// if(($pso['psalready']+$defaultpeisongliang)>=$pso['psvolume']){$dt['f_ps_psstatid']=1;}else{$dt['f_ps_psstatid']=4;}
		// //同时配送剩余时间也能算出来了
		// $arr_lineo=$line->getlineobylineid($pso['f_ps_lineid']);$lineo=$arr_lineo['data'];
		// $linespeed=$lineo['linespeed'];//瓶/分
		// $linespeed=$linespeed/$pso['itemctn'];//箱/分
        // $linespeed=$linespeed*$pso['partvolumepc'];//件/分

		// if(($pso['psalready']+$defaultpeisongliang)>=$pso['psvolume']){
		// 	$dt['psleavetm']=99999999;
		// }else{
		// 	$dt['psleavetm']=($pso['psremain']+$defaultpeisongliang)/$linespeed*60;
		// }

		// $dt['psremain']=$pso['psremain']+$defaultpeisongliang;
		// //同时也说明了plan已经在正儿八经配送了//当然还是有问题，有些物品没有正式配送，计算时间也停止，两边都没管上★
		// $get=array('planstat'=>3);
		// $plan->mdf($get,$pso['f_ps_planid']);
		// $ps->where('psid='.$psid)->setField($dt);
		// 
		$arr_pso=$this->getpsobypsid($psid);$pso=$arr_pso['data'];

		$arr_itemo=$item->getitemobyitemid($pso['f_ps_itemid']);$itemo=$arr_itemo['data'];

		$arr_plano=$plan->getplanobyplanid($pso['f_ps_planid']);$plano=$arr_plano['data'];

		$psalready=$pso['psalready']+$defaultpeisongliang;

		$arr_yuzhio=$yuzhi->getyuzhio($pso['f_ps_partid'],$pso['f_ps_lineid']);$yuzhio=$arr_yuzhio['data'];$linespeed=$yuzhio['yuzhispeed'];//瓶/每分钟
        $linespeed=$linespeed/$itemo['itemctn'];//箱/分钟

        $alreadyxiang=$psalready/$pso['partvolumepc'];
        $xianbiantm=($alreadyxiang-$plano['planrealvolume'])/$linespeed;
     
        $dt['psleavetm']=$xianbiantm;
		 
		$dt['psalready']=$psalready;

		$ps->where('psid='.$psid)->setField($dt);



		
		


		return createarrok('ok',$data,'',$info);
	}

	public function jisuanxianbiantm_psleavetm($psid){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL

		$item=D('Item');$plan=D('Plan');$yuzhi=D('Yuzhi');
		

		$arr_pso=$this->getpsobypsid($psid);$pso=$arr_pso['data'];

		$arr_itemo=$item->getitemobyitemid($pso['f_ps_itemid']);$itemo=$arr_itemo['data'];

		$arr_plano=$plan->getplanobyplanid($pso['f_ps_planid']);$plano=$arr_plano['data'];

		$psalready=$pso['psalready'];

		$arr_yuzhio=$yuzhi->getyuzhio($pso['f_ps_partid'],$pso['f_ps_lineid']);$yuzhio=$arr_yuzhio['data'];$linespeed=$yuzhio['yuzhispeed'];//瓶/每分钟
        $linespeed=$linespeed/$itemo['itemctn'];//箱/分钟

        $alreadyxiang=$psalready/$pso['partvolumepc'];

        if($alreadyxiang==0&&$plano['planrealvolume']==0){
        	$xianbiantm=0;//计算线边时间
        }else{
        	$xianbiantm=($alreadyxiang-$plano['planrealvolume'])/$linespeed;
        }
       
        
        

		return createarrok('ok',$xianbiantm,'',$info);
	}


	public function jisuanxianbiantm_psleavetm_nopsid($itemid,$planid,$psalready,$partid,$lineid,$partvolumepc){
		$info=collectinfo(__METHOD__,'$$itemid,$planid,$psalready,$partid,$lineid,$partvolumepc',array($itemid,$planid,$psalready,$partid,$lineid,$partvolumepc));
		
		
		$item=D('Item');$plan=D('Plan');$yuzhi=D('Yuzhi');
		

		

		$arr_itemo=$item->getitemobyitemid($itemid);$itemo=$arr_itemo['data'];

		$arr_plano=$plan->getplanobyplanid($planid);$plano=$arr_plano['data'];

		$arr_yuzhio=$yuzhi->getyuzhio($partid,$lineid);$yuzhio=$arr_yuzhio['data'];$linespeed=$yuzhio['yuzhispeed'];//瓶/每分钟
        $linespeed=$linespeed/$itemo['itemctn'];//箱/分钟

        $alreadyxiang=$psalready/$partvolumepc;
        if($alreadyxiang==0&&$plano['planrealvolume']==0){
        	$xianbiantm=0;//计算线边时间
        }else{
        	$xianbiantm=($alreadyxiang-$plano['planrealvolume'])/$linespeed;
        }
        
        

		return createarrok('ok',$xianbiantm,'',$info);
	}

	public function getpsobypsid($psid){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		

		$ps=M('ps');
		$pso=$ps->join('tb_plan ON f_ps_planid=planid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_item ON f_ps_itemid=itemid')->join('tb_part ON f_ps_partid=partid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where('psid='.$psid)->find();

		return createarrok('ok',$pso,'',$info);
	}	

	public function setpeisonging($psid){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		$ps=M('ps');
		$dt=array('f_ps_psstatid'=>4);
		$pso=$ps->where('psid='.$psid)->setField($dt);

		return createarrok('ok',$data,'',$info);
	}

	public function setpeisongcomplete($psid){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		$ps=M('ps');
		$dt=array('f_ps_psstatid'=>1);
		$pso=$ps->where('psid='.$psid)->setField($dt);

		return createarrok('ok',$data,'',$info);
	}

	public function setpeisongzhunbei($psid){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		$ps=M('ps');
		$dt=array('f_ps_psstatid'=>3);
		$pso=$ps->where('psid='.$psid)->setField($dt);

		return createarrok('ok',$data,'',$info);
	}	

	public function beiliao($lmt){
		$info=collectinfo(__METHOD__,'$lmt',array($lmt));
		
		if(isset($lmt)===false){return createarrerr('error_code','lmt 不能为空',$info);}//防止NULL
		$plan=D('Plan');

		$ps=M('ps');
		$dt=array('psodr'=>99999999);
		$ps->where('1=1')->setField($dt);

		$dt=array('f_ps_psstatid'=>4);
		$psls=$ps->join('tb_plan ON f_ps_planid=planid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_item ON f_ps_itemid=itemid')->join('tb_part ON f_ps_partid=partid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where('f_ps_psstatid<>1')->order('psodr ASC,psleavetm ASC')->limit('0,'.$lmt)->select();

		if($lmt>count($psls)){$truelmt=count($psls);}else{$truelmt=$lmt;}

		$i=0;
		foreach($psls as $psv){
			$i++;
			//部件配送中4
			$dt['psodr']=$i;
			$ps->where('psid='.$psv['psid'])->setField($dt);
			// //计划配送中3
			// $get=array('planstat'=>3);
			// $plan->mdf($get,$psv['f_ps_planid']);
		}

		return createarrok('ok',$truelmt,'',$info);
	}

	public function jieshubeiliao(){
		$info=collectinfo(__METHOD__,'',array($lmt));
		
		$ps=M('ps');
		$dt=array('psodr'=>99999999,'psclick'=>0);
		$ps->where('1=1')->setField($dt);

		return createarrok('ok',$data,'',$info);
	}

	public function changestatbyqueding($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		

		$ps=M('ps');
		$dt=array('f_ps_psstatid'=>2);
		$psls=$ps->where('f_ps_planid='.$planid)->select();
		foreach($psls as $psv){
			$ps->where('psid='.$psv['psid'])->setField($dt);
		}

		return createarrok('ok',$data,'',$info);
	}

	public function changestatbyquedingII($planid,$lineid){
		$info=collectinfo(__METHOD__,'$planid,$lineid',array($planid,$lineid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL

		$planls=M('plan')->where('f_plan_lineid='.$lineid.' AND planstat=2')->select();

		$ps=M('ps');
		$dt=array('f_ps_psstatid'=>2);
		$psls=$ps->where('f_ps_planid='.$planid)->select();
		foreach($psls as $psv){
			$ps->where('psid='.$psv['psid'])->setField($dt);
		}

		return createarrok('ok',$data,'',$info);
	}

	public function wancheng($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		

		$ps=M('ps');
		$psls=$ps->where('f_ps_planid='.$planid)->delete();
		
		return createarrok('ok',$data,'',$info);
	}

	public function getpslsbyplanid($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		$plan=D('Plan');

		
		$ps=M('ps');

		$psls=$ps->join('tb_plan ON f_ps_planid=planid')->join('tb_line ON f_ps_lineid=lineid')->join('tb_item ON f_ps_itemid=itemid')->join('tb_part ON f_ps_partid=partid')->join('tb_psstat ON f_ps_psstatid=psstatid')->where("f_ps_planid=".$planid." AND planstat<>1")->order('planstat DESC,planodr ASC')->select();
		
		return createarrok('ok',$psls,'',$info);
	}

	public function updateremainandtmbypsid($psid,$psremain){
		$info=collectinfo(__METHOD__,'$psid',array($psid));
		$info=collectinfo(__METHOD__,'$psremain',array($psremain));
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL
		if(isset($psremain)===false){return createarrerr('error_code','psremain 不能为空',$info);}//防止NULL
		$ps=M('ps');

		$arr_pso=$this->getpsobypsid($psid);$pso=$arr_pso['data'];
		$psleavetm=$psremain/$pso['linespeed']*60;
		$psleavetm=changeTimeType($psleavetm);
		
		$dt=array('psremain'=>$psremain,'psleavetm'=>$psleavetm);

		$ps->where('psid='.$psid)->setField($dt);

		return createarrok('ok',$data,'',$info);
	}

	public function deletepslsbyplanid($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		

		$ps=M('ps');

		$ps->where('f_ps_planid='.$planid)->delete();
		
		return createarrok('ok',$data,'',$info);
	}

	

	public function mdf($get,$psid){
		$info=collectinfo(__METHOD__,'$get,$psid',array($get,$psid));
		if(isset($get)===false){return createarrerr('error_code','get 不能为空',$info);}//防止NULL
		if(isset($psid)===false){return createarrerr('error_code','psid 不能为空',$info);}//防止NULL

		$ps=M('ps');
		$ps->where('psid='.$psid)->setField($get);

		return createarrok('ok',$data,'',$info);
	}

} 
?>